﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

namespace ETRoboConResultEditorExcel
{
    public partial class Sheet3
    {
        private static readonly int POS_TeamIdCol = 1;
        private static readonly int POS_TeamBibCol =2;
        private static readonly int POS_TeamNameCol = 3;
        private static readonly int POS_ModelPointCol = 4;
        private static readonly int POS_TotalRunPointCol = 5;
        private static readonly int POS_TotalPointCol = 6;
        private static readonly int POS_RankCol = 7;
        private static readonly int POS_EntryRow = 1;

        private static readonly int POS_TeamCountCol = 8;
        private static readonly int POS_TeamCountRow = 1;

        private void Sheet3_Startup(object sender, System.EventArgs e)
        {
        }

        private void Sheet3_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO で生成されたコード

        /// <summary>
        /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディタで変更しないでください。
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Sheet3_Startup);
            this.Shutdown += new System.EventHandler(Sheet3_Shutdown);
        }

        #endregion

        public void AddTeamResult(int bib, long teamId, string name, double? modelPoint, int? totalRunPoint, double? totalPoint, int? rank)
        {
            int current = GetTeamEntryCount(true);
            ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + current, POS_TeamIdCol]).Value2 = teamId;
            ((Excel.Range)(Globals.Sheet3.Cells[POS_EntryRow + current, POS_TeamBibCol])).Value2 = bib;
            ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + current, POS_TeamNameCol]).Value2 = name;
            ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + current, POS_ModelPointCol]).Value2 = modelPoint;
            ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + current, POS_TotalRunPointCol]).Value2 = totalRunPoint;
            ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + current, POS_TotalPointCol]).Value2 = totalPoint;
            ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + current, POS_RankCol]).Value2 = rank;
        }

        public int GetTeamResult(out int bib,out string teamId, out string name, out int? modelPoint, out int? totalRunPoint, out int? rank, ref int index)
        {
            bib = 0;
            teamId = "";
            name = "";
            modelPoint = null;
            totalRunPoint = null;
            rank = null;
            int current = GetTeamEntryCount(false);
            if (index < current)
            {
                teamId = ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + index + 1, POS_TeamIdCol]).Value2.ToString();

                string bibStr = ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + index + 1, POS_TeamBibCol]).Value2.ToString();
                Int32.TryParse(bibStr, out bib);
                name = ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + index + 1, POS_TeamNameCol]).Value2.ToString();
                if (((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + index + 1, POS_ModelPointCol]).Value2 != null)
                {
                    string modelPointStr = ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + index + 1, POS_ModelPointCol]).Value2.ToString();
                    int modelPointNN;
                    double modelPointDBL;
                    double.TryParse(modelPointStr, out modelPointDBL);
                    modelPointNN= (int)System.Math.Round(modelPointDBL*100);
          //          Int32.TryParse(modelPointStr, out modelPointNN);
                    modelPoint = modelPointNN;
                }
                if (((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + index + 1, POS_TotalRunPointCol]).Value2 != null)
                {
                    string totalRunPointStr = ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + index + 1, POS_TotalRunPointCol]).Value2.ToString();
                    int totalRunPointNN;
                    Int32.TryParse(totalRunPointStr, out totalRunPointNN);
                    totalRunPoint = totalRunPointNN;
                }
                if (((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + index + 1, POS_RankCol]).Value2 != null)
                {
                    string rankStr = ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + index + 1, POS_RankCol]).Value2.ToString();
                    int rankNN;
                    Int32.TryParse(rankStr, out rankNN);
                    rank = rankNN;
                }
                return ++index;
            }
            return -1;
        }

        private int GetTeamEntryCount(bool increment)
        {
            string current = ((Excel.Range)(Globals.Sheet3.Cells[POS_TeamCountRow, POS_TeamCountCol])).Value2.ToString();
            int result = -1;
            if (Int32.TryParse(current, out result))
            {
                if (increment) result++;
                ((Excel.Range)(Globals.Sheet3.Cells[POS_TeamCountRow, POS_TeamCountCol])).Value2 = result;
            }
            return result;
        }

        public void ClearTeamsResults()
        {
            int current = GetTeamEntryCount(false);
            for (int i = 1; i <= current; i++)
            {
                ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + i, POS_TeamIdCol]).Value2 = null;
                ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + i, POS_TeamBibCol]).Value2 = null;
                ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + i, POS_TeamNameCol]).Value2 = null;
                ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + i, POS_ModelPointCol]).Value2 = null;
                ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + i, POS_TotalRunPointCol]).Value2 = null;
                ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + i, POS_TotalPointCol]).Value2 = null;
                ((Excel.Range)Globals.Sheet3.Cells[POS_EntryRow + current, POS_RankCol]).Value2 = null;
            }
            ((Excel.Range)Globals.Sheet3.Cells[POS_TeamCountRow, POS_TeamCountCol]).Value2 = 0;
        }
    }
}
